/*******************************************************************************
 * Copyright (c) 2008-2010 Cedric Vidal
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cedric Vidal - original idea, initial contribution and API
 *******************************************************************************/
package org.trans4j.impl;

import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.util.Collection;

import org.junit.Test;

import static org.trans4j.junit.MoreAsserts.*;

public class HasherTest {

	private Hasher hasher = new Hasher();

	@Test
	public void hashDifferentArraysWithSameContentReturnsSameHash() {
		String b = "b";
		String a = "a";
		assertEquals(hasher.hash(new String[] {a, b}), hasher.hash(new String[] {a, b}));
	}

	@Test
	public void hashDifferentArraysWithDifferentContentReturnsDifferentHash() {
		int h1 = hasher.hash(new Clazz[] {new Clazz()});
		int h2 = hasher.hash(new Clazz[] {new Clazz()});
		assertNotEquals(h1, h2);
	}

	@Test
	public void hashSameMethodWithSameContentReturnsSameHash() throws Exception {
		Method method = TestMapper.class.getMethod("map", String.class);
		String b = "b";
		String a = "a";
		assertEquals(hasher.hash(method, new String[] {a, b}), hasher.hash(method, new String[] {a, b}));
	}

	@Test
	public void hashSameMethodWithDifferentContentReturnsDifferentHashes() throws Exception {
		Method method = TestMapper.class.getMethod("map", String.class);
		assertNotEquals(hasher.hash(method, new Clazz[] {new Clazz()}), hasher.hash(method, new Clazz[] {new Clazz()}));
	}

	@Test
	public void hashSameMethodWithSameContentFromDifferentMapperClassesReturnsDifferentHashes() throws Exception {
		Method method1 = TestMapper.class.getMethod("map", String.class);
		Method method2 = TestMapper2.class.getMethod("map", String.class);
		Clazz[] args = new Clazz[] {new Clazz()};
		assertNotEquals(hasher.hash(method1, args), hasher.hash(method2, args));
	}

	@Test
	public void hashSameMethodWithElementOrCollectionReturnsDifferentHashes() throws Exception {
		Method methodElement = TestMapper.class.getMethod("map", String.class);
		Method methodCollection = TestMapper.class.getMethod("map", Collection.class);
		Clazz[] args = new Clazz[] {new Clazz()};
		assertNotEquals(hasher.hash(methodElement, args), hasher.hash(methodCollection, args));
	}

	static class Clazz {}

	static class TestMapper {
		public String map(String a) {
			return a + "Map";
		}
		public String map(Collection<String> a) {
			return "Collection " + a + " Map";
		}
	}

	static class TestMapper2 {
		public String map(String a) {
			return a + "Map2";
		}
	}

}
